import 'package:flutter_admin/models/user_info.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/models/odoo/stock_picking.dart';
import 'package:flutter_admin/utils/store_util.dart';
import 'package:flutter_admin/utils/utils.dart';
import 'package:intl/intl.dart';
import 'package:quiver/strings.dart';
import '../../../../generated/l10n.dart';

class StockPickingFinishedDialog extends StatefulWidget {
  StockPickingFinishedDialog();

  @override
  _StockPickingFinishedDialogState createState() =>
      _StockPickingFinishedDialogState();
}

class _StockPickingFinishedDialogState
    extends State<StockPickingFinishedDialog> {
  // late List<Widget> children;
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  StockPicking stockPicking = StockPicking();

  DateFormat format = DateFormat('yyyy-MM-dd');
  //车间
  List<SelectOptionVO> workshopList = [];
  //源库房
  List<SelectOptionVO> locationList = [];

  UserInfo userInfo = StoreUtil.getCurrentUserInfo();

  @override
  void initState() {
    super.initState();
    stockPicking.createDateStart = Utils.getDate();
    stockPicking.createDateEnd = Utils.getDate();
    _loadSelectVO();
  }

  _loadSelectVO() async {
    if (userInfo.major != null) {
      workshopList = await DynamicDataApi.getModelSelect(
          model: "hr.department",
          name: "complete_name",
          key: "parent_id",
          operator: "!=",
          val: null);
    } else {
      locationList = await DynamicDataApi.getModelSelect(
          active: false,
          model: "stock.location",
          name: "complete_name",
          key: 'workshop_id',
          val: int.parse(userInfo.deptId!),
          key2: 'comment',
          val2: "CPK");
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelectDate(
            context,
            label: '入库开始时间',
            value: stockPicking.createDateStart,
            lastDate: isNotBlank(stockPicking.createDateEnd)
                ? format.parse(stockPicking.createDateEnd!)
                : null,
            onDateChange: (v) {
              formKey.currentState?.save();
              stockPicking.createDateStart = v;
              setState(() {});
            },
            onSaved: (v) {
              stockPicking.createDateStart = v;
            },
          ),
          CrySelectDate(
            context,
            label: '入库结束时间',
            value: stockPicking.createDateEnd,
            firstDate: isNotBlank(stockPicking.createDateStart)
                ? format.parse(stockPicking.createDateStart!)
                : null,
            onDateChange: (v) {
              formKey.currentState?.save();
              stockPicking.createDateEnd = v;
              setState(() {});
            },
            onSaved: (v) {
              stockPicking.createDateEnd = v;
            },
          ),
          userInfo.major != null
              ? CrySelect(
            label: '生产车间',
            dataList: workshopList,
            value: stockPicking.workshopId,
            onSaved: (v) {
              stockPicking.workshopId = v;
            },
            onChange: (v) async {
              if (stockPicking.workshopId != v) {
                formKey.currentState!.save();
                stockPicking.workshopId = v;
                stockPicking.locationId = null;
                try {
                  locationList = await DynamicDataApi.getModelSelect(
                      active: false,
                      model: "stock.location",
                      name: "complete_name",
                      key: 'workshop_id',
                      val: int.parse(v),
                      key2: 'comment',
                      val2: "CPK");
                } catch (e) {
                  locationList = [];
                }
                setState(() {});
              }
            },
          )
              : CryInput(
            label: '生产车间',
            enable: false,
            value: userInfo.deptName,
          ),
          CrySelect(
            label: '库房',
            dataList: locationList,
            value: stockPicking.locationId,
            onSaved: (v) {
              stockPicking.locationId = v;
            },
          ),
          CryInput(
            label: '物料编码',
            value: stockPicking.productCode,
            onSaved: (v) {
              stockPicking.productCode = v;
            },
          ),
          CryInput(
            label: '物料名称',
            value: stockPicking.productName,
            onSaved: (v) {
              stockPicking.productName = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            form.save();
            Navigator.pop(context, stockPicking);
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("查询"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
